package com.mm.cloth.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.mm.cloth.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片预览适配器，用于轮播图和图片预览
 */
public class PreviewAdapter extends RecyclerView.Adapter<PreviewAdapter.PreviewViewHolder> {

    private Context context;
    private List<String> images = new ArrayList<>();

    public PreviewAdapter(Context context) {
        this.context = context;
    }

    public void setImages(List<String> images) {
        this.images = images;
    }

    @NonNull
    @Override
    public PreviewViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_preview, parent, false);
        return new PreviewViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PreviewViewHolder holder, int position) {
        String imageUrl = images.get(position);
        
        // 使用Glide加载图片
        Glide.with(context)
                .load(imageUrl)
                .transition(DrawableTransitionOptions.withCrossFade())
                .placeholder(R.drawable.empty_wardrobe)
                .error(R.drawable.empty_wardrobe)
                .centerCrop()
                .into(holder.imageView);
    }

    @Override
    public int getItemCount() {
        return images.size();
    }

    static class PreviewViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;

        public PreviewViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.ivPreview);
        }
    }
} 